/*******************************************************************************
 * Copyright 2011 Hani Naguib
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.gvmax.api;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.io.IOUtils;

/**
 * Miscellaneus utilities
 * 
 * @author Hani Naguib
 */
class MiscUtil {

	public static void validateNotEmpty(String name, Object value) throws IllegalArgumentException {
		if (value == null)
			throw new IllegalArgumentException(name + " cannot be null");
		if (value.toString().trim().equals(""))
			throw new IllegalArgumentException(name + " cannot be empty");
	}
	
	// ------------------
	// NET UTILS
	// ------------------
	
	public static String doGet(String url) throws IOException {
		URL u = new URL(url);
		URLConnection conn = u.openConnection();
		conn.setReadTimeout(5000);
		conn.setConnectTimeout(5000);
		InputStream in = conn.getInputStream();
		try {
			return IOUtils.toString(in,"utf8");
		} finally {
			IOUtils.closeQuietly(in);
		}
	}

	public static String doPost(String url, Map<String, String> params) throws IOException {
		String paramsStr = "";
		Iterator<String> paramNames = params.keySet().iterator();
		if (paramNames.hasNext()) {
			String paramName = paramNames.next();
			paramsStr = paramName + "=" + URLEncoder.encode(params.get(paramName),"utf8");
		}
		while (paramNames.hasNext()) {
			String paramName = paramNames.next();
			paramsStr += "&"+paramName + "=" + URLEncoder.encode(params.get(paramName),"utf8");
		}
		byte[] postData = paramsStr.getBytes("utf8");
		
		
		URL u = new URL(url);

		HttpURLConnection conn = (HttpURLConnection) u.openConnection();
		conn.setReadTimeout(10000);
		conn.setConnectTimeout(10000);
		conn.setDoOutput(true);
		conn.setUseCaches(false);
		conn.setRequestMethod("POST");
		conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		conn.setRequestProperty("Content-Length", Integer.toString(postData.length));

		OutputStream out = conn.getOutputStream();
		try {
			out.write(postData);
		} finally {
			IOUtils.closeQuietly(out);
		}
		
		InputStream in = conn.getInputStream();
		try {
			return IOUtils.toString(in,"utf8");
		} finally {
			IOUtils.closeQuietly(in);
		}
	}

	
}
